package com.lambo.maven.core.utils.superHttp.utils;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;

import com.lambo.maven.core.utils.superHttp.SHttp;
import com.lambo.maven.core.utils.superHttp.core.ProxyProp;
import com.lambo.maven.core.utils.superHttp.entity.SHttpConfig;

/**
 * 构建器
 */
public class SHttpBuilder {

    /**
     * 设置代理
     * @param builder
     * @param shttpConfig
     */
    public static void proxy(HttpClientBuilder builder, SHttpConfig shttpConfig) {
        if(shttpConfig != null && shttpConfig.isNoneProxy()) {
            return;
        }
        String host = ProxyProp.getProxyHost();
        Integer port = ProxyProp.getProxyPort();
        String name = ProxyProp.getUsername();
        String pwd = ProxyProp.getPassword();
        
        HttpHost proxy = new HttpHost(host, port, "http");
        builder.setProxy(proxy);
        
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
            AuthScope authScope = new AuthScope(host, port);
            Credentials credentials = new UsernamePasswordCredentials(name, pwd);
            credsProvider.setCredentials(authScope, credentials);
        builder.setDefaultCredentialsProvider(credsProvider);
    }

    /**
     * 设置代理
     * @param httpRequestBase
     */
    public static void proxy(HttpRequestBase httpRequestBase) {
        // SHttp shttp = SHttpDefault.getSystem();
        // httpRequestBase.setConfig(shttp.getProxyRequestConfig());
    }
    
}
